package com.lzyyj.gmis.base.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lzyyj.common.BeanUtilsExt;
import com.lzyyj.common.Page;
import com.lzyyj.gmis.base.dao.IBaseDao;
import com.lzyyj.gmis.base.model.IMenu;
import com.lzyyj.gmis.base.model.Menu;

@SuppressWarnings("serial")
public class MenuService<PK extends Serializable> extends BaseService<IMenu<PK>, PK> implements IMenuService<PK> {

	private static final Log log = LogFactory.getLog(MenuService.class);
	
	public static final String DEFAULT_MENU_TYPE = "系统主菜单";

	@Override
	public List<IMenu<PK>> getMenus() throws Exception {
		return getMenusBy(DEFAULT_MENU_TYPE);
	}
	
	@Override
	public Page<IMenu<PK>> manage(Page<IMenu<PK>> page) throws Exception {
		Page<IMenu<PK>> result = null;
		
		IMenu<PK> example = page.getFindByExample();
		if (example==null){
			example=new Menu();
			page.setFindByExample(example);
		}
		String exampleProperties = (String)page.getCondition().get(IBaseDao.EXAMPLE_PROPERTIES);
		if (exampleProperties==null||exampleProperties.length()==0){
			IMenu<PK> root = getMenuByName(DEFAULT_MENU_TYPE);
			example.setParent(root);
			page.getCondition().put(IBaseDao.EXAMPLE_PROPERTIES, "parent");
		}
		
		String orderFields = page.getOrderFields();
		if (orderFields==null||orderFields.length()==0){
			orderFields="parent,orderNo";
			page.setOrderFields(orderFields);
		}
		result = super.manage(page);
		List<IMenu<PK>> list = new ArrayList<IMenu<PK>>();
		
		return result;
	}
	
	private UserService userService;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<IMenu<PK>> getMainMenus() throws Exception{
		return getMenusBy(DEFAULT_MENU_TYPE);
	}

	public List<IMenu<PK>> getSystemTools() throws Exception{
		return getMenusBy("系统工具栏");
	}
	
	/**
	 * 获取指定名称的菜单，仅限于根菜单
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public IMenu<PK> getMenuByName(String name) throws Exception{
		IMenu<PK> menu = null;
		
		if (name!=null&&name.length()>0){
			IMenu<PK> example = new Menu();
			example.setName(name);
			List<IMenu<PK>> menus = findByExample(example, "parent,=name", "orderNo", 0,1);
			if (menus.size()>0){
				log.debug("找到指定菜单类型！");
				Object o = menus.get(0);
				if (o instanceof IMenu){
					log.debug("类型匹配！");
					menu = menus.get(0);
				}else{
					log.debug("类型不匹配！");
					menu = new Menu();
					
					BeanUtilsExt.copyProperties(o, menu, "id,parent,orderNo,name,menus");
				}
			}
		}
		
		return menu;
	}

		
	/**
	 * 获取指定类型的菜单，缺省值“系统主菜单”
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public List<IMenu<PK>> getMenusBy(String name) throws Exception{
		List<IMenu<PK>> mainMenus = new ArrayList<IMenu<PK>>();
		
		Object currentUser = null;
		if (getUserService()!=null){
			currentUser = userService.getCurrentUser();
		}
		
		if (name==null||name.length()==0)name = DEFAULT_MENU_TYPE;
		IMenu<PK> menu = getMenuByName(name);
		if (menu!=null){
			List<IMenu<PK>> menus = menu.getMenus();
			log.debug("获取一级菜单："+menus.size()+"项");
			// 遍历一级菜单
			for(IMenu<PK> mainMenu:menus){
				menu = new Menu();
				menu.setId(mainMenu.getId());
				menu.setOrderNo(mainMenu.getOrderNo());
				menu.setName(mainMenu.getName());
				menu.setDescription(mainMenu.getDescription());
				menu.setUrl(mainMenu.getUrl());
				menu.setIcon(mainMenu.getIcon());
				menu.setBackgroundImage(mainMenu.getBackgroundImage());
				menu.setMenuImage(mainMenu.getMenuImage());
				
				log.debug("获取二级子菜单："+mainMenu.getMenus().size());
				IPurviewService<? extends Serializable> purviewService = getSystemService().getPurviewService();
				if (purviewService!=null){
					// 有权限管理模块
					for(IMenu<PK> subMenu:mainMenu.getMenus()){
						// 权限检查
						subMenu.setShowEnabled(subMenu.getPurviewKey()==null||currentUser!=null&&purviewService.checkCode(subMenu.getPurviewKey(), currentUser));
						// 有权限的话添加到菜单列表
						if (subMenu.isShowEnabled()){
							log.debug("添加菜单项："+subMenu.getName());
							menu.getMenus().add(subMenu);
						}
					}
				}else{
					for(IMenu<PK> subMenu:mainMenu.getMenus()){
						// 权限检查
						subMenu.setShowEnabled(true);
						// 有权限的话添加到菜单列表
						if (subMenu.isShowEnabled()){
							log.debug("添加菜单项："+subMenu.getName());
							menu.getMenus().add(subMenu);
						}
					}
				}
				// 如果有子菜单项才添加到主菜单列表
				if (menu.getMenus().size()>0){
					log.debug("添加主菜单项："+menu);
					mainMenus.add(menu);
				}
			}
		}
		
		return mainMenus;
	}

}
